/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-15
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "string.h"
#include "ls_ble_log.h"
#include "ls_ble_dis.h"
#include "ls_ble_adpter.h"
#include "ls_ble_sdk_api.h"
#include "ls_ble_sdk_config.h"
#include "ls_ble_file_api.h"
#include "ls_ble_custom_api.h"
#include "ls_ble_file_protocol.h"
#include "ls_ble_custom_protocol.h"
#include "ls_ble_phy62xx.h"
#include "user_config.h"

#include "sys_task.h"
#include "app_global_data.h"
#include "app_store_set_info.h"

#define BLE_SDK_API_LOG_EN  1
#if BLE_SDK_API_LOG_EN
#include "ls_ble_log.h"
#define TAG                             "sdk_api"
#define BLE_SDK_API_LOG(level, ...)     LS_BLE_LOG_PRINT(level, TAG, ##__VA_ARGS__)
#else
#define BLE_SDK_API_LOG(level, ...)
#endif

/*----------------------------macro file---------------------------*/

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/							

/*-------------------------func declaration------------------------*/
extern void ota_protocol_init(void);
extern void set_cur_clr_secs_cnt(void);

/*-----------------------------------------------------------------*/

static void ls_ble_sdk_log_init(void)
{
#if LS_BLE_LOG_ENABLE
    ls_ble_log_output_api_cfg(ls_ble_adpter_log_putc, NULL);
    
    ls_ble_log_init_t log_init;

    log_init.filter.level             = LOG_LVL_DEBUG;
    log_init.fmt_set[LOG_LVL_ERROR]   = LS_BLE_LOG_FMT_ALL & (~LS_BLE_LOG_FMT_TAG);
    log_init.fmt_set[LOG_LVL_WARNING] = LS_BLE_LOG_FMT_LVL | LS_BLE_LOG_FMT_TAG;
    log_init.fmt_set[LOG_LVL_INFO]    = LS_BLE_LOG_FMT_LVL | LS_BLE_LOG_FMT_TAG;
    log_init.fmt_set[LOG_LVL_DEBUG]   = LS_BLE_LOG_FMT_LVL | LS_BLE_LOG_FMT_TAG;

    ls_ble_log_init(&log_init);
#endif
}


static void dis_info_init(void)
{
    ls_ble_mac_t mac;
    uint8_t sys_id[8];
    
    memset(sys_id, 0, sizeof(sys_id));
    ls_ble_get_mac(&mac);
    sys_id[0] = mac.addr[0];
    sys_id[1] = mac.addr[1];
    sys_id[2] = mac.addr[2];
    sys_id[5] = mac.addr[3];
    sys_id[6] = mac.addr[4];
    sys_id[7] = mac.addr[5];
    dis_set_param(DIS_IDX_SYSTEM_ID, sizeof(sys_id), sys_id);
}


void ls_ble_sdk_service_init(void)
{
    ls_ble_mac_t mac;

    dis_info_init();
    dis_service_init();
    custom_protocol_init();
    ota_protocol_init();
#if (LS_BLE_FILE_SVC_ENABLE)
    file_protocol_init();
#endif
    ls_ble_sdk_log_init();
    
#if(DEV_MAC_BURN_ENABLE)
    uint8_t tmp_mac[DEV_MAC_LEN];
	if(DEV_MAC_LEN == app_set_info_burn_mac_get(tmp_mac))
	{
        mac.type = 0;
        memcpy(mac.addr, tmp_mac, DEV_MAC_LEN);
        ls_ble_set_mac(&mac);
        g_app_data.sys.state.dev_mac_valid = true;
	}
#endif
    
    ls_ble_set_device_name((uint8_t*)BLE_ADV_DEV_NAME, strlen(BLE_ADV_DEV_NAME));
    ls_ble_set_conn_param(BLE_CONN_INTERVAL_MIN, BLE_CONN_INTERVAL_MAX, BLE_SLAVE_LATENCY, BLE_CONN_SUP_TIMEOUT);

    ls_ble_adv_param_t param;
    param.adv_interval_min = BLE_ADV_FAST_INV_MS;
    param.adv_interval_max = BLE_ADV_FAST_INV_MS;
    ls_ble_set_adv_param(&param);
    
    if(false == g_app_data.sys.state.pcb_test) {
        /*uint8_t name_buf[APP_DEVICE_NAME_MAX_LEN];
        uint8_t name_len = app_set_info_ble_adv_name_get(name_buf);
        if(name_len > 0) {
            ls_ble_set_advdata_local_name(name_buf, name_len);
        }
        else {*/
            ls_ble_set_advdata_local_name((uint8_t*)BLE_ADV_DEV_NAME, BLE_ADV_DEV_NAME_LEN);
        //}
    }
    else {
        ls_ble_set_advdata_local_name((uint8_t*)MODULE_NUM_STR, strlen(MODULE_NUM_STR));
    }
    

    ls_ble_get_mac(&mac);
    uint8_t scan_data[ADV_MANUFACTORY_DATA_LEN+1];
    ls_ble_get_scanrsp_manufac_data(scan_data, sizeof(scan_data));
    memcpy(&scan_data[4], mac.addr, 6);
#if(LEIJISHU == 0)
    #if(DEV_MAC_BURN_ENABLE)
    scan_data[23] = g_app_data.sys.state.dev_mac_valid;
    #endif
#else   //#if(LEIJISHU == 0)
    extern uint8_t dev_adv_state_get(void);
    scan_data[17] = dev_adv_state_get();
#endif  //#if(LEIJISHU == 0)
    ls_ble_set_scanrsp_manufac_data(scan_data, sizeof(scan_data));
}


#if(LEIJISHU == 0)
void ls_ble_udpate_skip_scan_data(void)
{
    if(true == g_app_data.sys.state.ble_conn)
        return;

    uint32_t utc = 0;
    static uint8_t adv_skip_mode = 0;
    static uint16_t adv_skip_setting = 0;
    static uint16_t adv_total_secs = 0, adv_total_cnts = 0, adv_valid_sec = 0;


    if(adv_total_secs == g_app_data.tmp.skip.total_secs \
        && adv_total_cnts == g_app_data.tmp.skip.total_cnts \
        && adv_skip_mode == g_app_data.tmp.skip.mode \
        && adv_skip_setting == g_app_data.tmp.skip.setting)
        return;

    if(true == g_app_data.sys.state.skip_done) {
        utc = g_app_data.tmp.skip.start_utc;
    }

    adv_skip_mode = g_app_data.tmp.skip.mode;
    adv_skip_setting = g_app_data.tmp.skip.setting;
    adv_total_secs = g_app_data.tmp.skip.total_secs;
    adv_total_cnts = g_app_data.tmp.skip.total_cnts;
    adv_valid_sec = g_app_data.tmp.skip.skip_valid_sec;

    uint8_t scan_data[ADV_MANUFACTORY_DATA_LEN+1];
    ls_ble_get_scanrsp_manufac_data(scan_data, sizeof(scan_data));

    scan_data[10] = adv_skip_mode;
    memcpy(&scan_data[11], &adv_skip_setting, sizeof(uint16_t));
    memcpy(&scan_data[13], &utc, sizeof(uint32_t));
    memcpy(&scan_data[17], &adv_total_secs, sizeof(uint16_t));
    memcpy(&scan_data[19], &adv_total_cnts, sizeof(uint16_t));
    memcpy(&scan_data[21], &adv_valid_sec, sizeof(uint16_t));
#if (SCHOOL_ADV_ENABLE)
    scan_data[24] = g_app_data.tmp.trip_group[0];
    scan_data[25] = g_app_data.tmp.skip_group[0];
    scan_data[26] = g_app_data.tmp.trip_group[1];
    scan_data[27] = g_app_data.tmp.skip_group[1];
    scan_data[28] = g_app_data.tmp.trip_group[2];
    scan_data[29] = g_app_data.tmp.skip_group[2];
#endif

    ls_ble_set_scanrsp_manufac_data(scan_data, sizeof(scan_data));
}

#else   //#if(LEIJISHU == 0)

extern uint32_t trip_record;

uint8_t dev_adv_state_get(void)
{
    app_dev_adv_state_t state;
    memset(&state, 0, sizeof(state));
    if(g_app_data.sys.state.dev_mac_valid) {
        state.mac_valid = 1;
    }
    if(true == g_app_data.sys.state.skip_done) {
        state.skip_state = ADV_SKIP_STATE_DONE;
    }
    else {
        if(g_app_data.tmp.skip.total_cnts <= 0) {
            state.skip_state = ADV_SKIP_STATE_IDLE;
        }
        else {
            state.skip_state = ADV_SKIP_STATE_SKIPING;
        }
    }
    
    uint8_t tmp = 0;
    memcpy(&tmp, &state, sizeof(tmp));
    
    return tmp;
}

void ls_ble_udpate_skip_scan_data(void)
{    
    if(true == g_app_data.sys.state.ble_conn)
        return;

    uint32_t utc = g_app_data.tmp.cumulative_num_utc;
    static uint8_t adv_skip_mode = 0;
    static uint16_t adv_total_cnts = 0;

    adv_skip_mode = g_app_data.tmp.skip.mode;
    if(adv_total_cnts != g_app_data.tmp.skip.total_cnts) {
        set_cur_clr_secs_cnt();
    }
    adv_total_cnts = g_app_data.tmp.skip.total_cnts;

    uint8_t scan_data[31];
    ls_ble_get_scanrsp_manufac_data(scan_data, sizeof(scan_data));

    scan_data[10] = adv_skip_mode;
    memcpy(&scan_data[11], &utc, sizeof(uint32_t));
    memcpy(&scan_data[15], &adv_total_cnts, sizeof(uint16_t));
    scan_data[17] = dev_adv_state_get();

    memcpy(&scan_data[18], &g_app_data.tmp.skip_record, SKIP_RECORD_MAX_NUM);
    scan_data[28] = (trip_record >> 16) & 0xFF;
    scan_data[29] = (trip_record >> 8) & 0xFF;
    scan_data[30] = trip_record & 0xFF;

    ls_ble_set_scanrsp_manufac_data(scan_data, sizeof(scan_data));
}

#endif


void ls_ble_sdk_callback(ls_ble_sdk_cb_evt_param_t* param)
{
    switch(param->evt)
    {
        case LS_BLE_SDK_CB_EVT_CONNECT: {
            LS_BLE_LOG_RAW_INFO("Connected\n");
            g_app_data.sys.state.ble_conn = true;
        } break;
        
        case LS_BLE_SDK_CB_EVT_DISCONNECT: {
            LS_BLE_LOG_RAW_INFO("Disconnected\n");
            g_app_data.sys.state.ble_conn = false;
            g_app_data.sys.ble_conn_type = BLE_CONN_TYPE_NULL;
            
            if(DEV_MODE_SLEEP != g_app_data.sys.mode) {
                ls_ble_start_adv();
            }
            
            ls_ble_cus_cb_evt_param_t cus_evt_param;
            cus_evt_param.evt = LS_BLE_CUS_CB_EVT_DISCONNECT;
            ls_ble_custom_callback(&cus_evt_param);
            
#if (LS_BLE_FILE_SVC_ENABLE)
            ls_ble_file_cb_evt_param_t file_evt_param;
            file_evt_param.evt = LS_BLE_FILE_CB_EVT_DISCONNECT;
            ls_ble_file_callback(&file_evt_param);
#endif
        } break;
        
        case LS_BLE_SDK_CB_EVT_OTA_START: {
            if(DEV_MODE_OTA_ING != g_app_data.sys.mode) {
                g_app_data.sys.mode = DEV_MODE_OTA_ING;
                sys_task_msg_send(MSG_TYPE_UI, UI_EVT_OTA_ING);
            }
        } break;
        
        case LS_BLE_SDK_CB_EVT_OTA_DONE: {
            if(DEV_MODE_OTA_ING == g_app_data.sys.mode) {
                g_app_data.sys.mode = DEV_MODE_OTA_DONE;
                sys_task_msg_send(MSG_TYPE_UI, UI_EVT_OTA_DONE);
            }
        } break;
        
        case LS_BLE_SDK_CB_EVT_OTA_EXIT: {
            if(DEV_MODE_OTA_ING == g_app_data.sys.mode) {
                g_app_data.sys.mode = DEV_MODE_OTA_FAIL;
                sys_task_msg_send(MSG_TYPE_UI, UI_EVT_OTA_FAIL);
            }
        } break;
        
        default:
            break;
    }
}



